package com.fan.shop.services;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.transaction.annotation.Transactional;

import com.fan.shop.dao.StockReqDao;
import com.fan.shop.dao.StockReqLineDao;
import com.fan.shop.domain.StockReq;
import com.fan.shop.domain.StockReqLine;
import com.fan.shop.model.userclass.StockReqLineUserClass;
import com.fan.shop.model.userclass.StockReqUserClass;

@Named("stockReqService")
@Transactional(readOnly = true)
public class StockReqServiceImpl implements StockReqService {

	@Inject
	@Named("stockReqDao")
	private StockReqDao stockReqDao;

	public StockReqDao getStockReqDao() {
		return stockReqDao;
	}

	public void setStockReqDao(StockReqDao stockReqDao) {
		this.stockReqDao = stockReqDao;
	}

	public StockReqLineDao getStockReqLineDao() {
		return stockReqLineDao;
	}

	public void setStockReqLineDao(StockReqLineDao stockReqLineDao) {
		this.stockReqLineDao = stockReqLineDao;
	}

	@Inject
	@Named("stockReqLineDao")
	private StockReqLineDao stockReqLineDao;

	@SuppressWarnings("rawtypes")
	public List getList() {
		List<StockReqUserClass> list = new ArrayList<StockReqUserClass>();

		for (StockReq stockReq : stockReqDao.getAllStockReqs()) {
			list.add(copy(stockReq));
		}
		return list;
	}

	public int getColumnsSize() {
		return stockReqDao.getColumnNames().size();
	}

	public StockReqUserClass copy(Object obj) {

		StockReq stockReq = (StockReq) obj;
		StockReqUserClass stockReqUserClass = new StockReqUserClass();
		stockReqUserClass.setRequisitionNo(stockReq.getRequisitionNo());
		stockReqUserClass.setStoreCode(stockReq.getStoreCode());
		stockReqUserClass.setOfficerCode(stockReq.getOfficerCode());
		stockReqUserClass.setTransactionCode(stockReq.getTransactionCode());
		stockReqUserClass.setTransactionDate(stockReq.getTransactionDate()
				.toString());
		if (stockReq.getReceivedDate() != null) {
			stockReqUserClass.setReceivedDate(stockReq.getReceivedDate()
					.toString());
		}
		stockReqUserClass.setPostage(stockReq.getPostage());
		stockReqUserClass.setStatus(stockReq.getStatus());
		stockReqUserClass.setComment(stockReq.getComment());

		return stockReqUserClass;
	}

	public StockReq getStockReq(String requisitionNo) {
		return stockReqDao.getStockReq(requisitionNo);
	}

	public List<StockReq> getAllStockReqs() {
		return stockReqDao.getAllStockReqs();
	}

	public int getSize() {
		return getAllStockReqs().size();
	}

	@Transactional(readOnly = false)
	public void addStockReq(StockReq stockReq) {
		stockReqDao.addStockReq(stockReq);
	}

	@Transactional(readOnly = false)
	public void updateStockReq(StockReq stockReq) {
		stockReqDao.updateStockReq(stockReq);

	}

	@Transactional(readOnly = false)
	public void deleteStockReq(StockReq stockReq) {
		stockReqDao.deleteStockReq(stockReq);

	}

	public boolean hasStockReq(String requisitionNo) {
		return stockReqDao.hasStockReq(requisitionNo);
	}

	public List<String> getColumnNames() {
		return stockReqDao.getColumnNames();
	}

	public BigDecimal getTotalPrice(String requisitionNo) {
		return stockReqDao.getTotalPrice(requisitionNo);
	}

	public int getTotalQty(String requisitionNo) {
		return stockReqDao.getTotalQty(requisitionNo);
	}

	public List<StockReqLine> getStockReqLines(String requisitionNo) {
		return stockReqDao.getStockReqLines(requisitionNo);
	}

	public List<StockReqLineUserClass> getStockReqLinesUserClasses(
			String requisitionNo) {
		List<StockReqLineUserClass> list = new ArrayList<StockReqLineUserClass>();
		for (StockReqLine stockReqLine : getStockReqLines(requisitionNo)) {
			StockReqLineUserClass stockReqLineUserClass = copyToLineUSerClass(stockReqLine);
			list.add(stockReqLineUserClass);

		}
		return list;
	}

	public StockReqLineUserClass copyToLineUSerClass(Object obj) {
		StockReqLine stockReqLine = (StockReqLine) obj;

		StockReqLineUserClass stockReqLineUserClass = new StockReqLineUserClass();
		stockReqLineUserClass.setRequisitionNo(stockReqLine.getId()
				.getRequisitionNo());
		stockReqLineUserClass.setItemCode(stockReqLine.getId().getItemCode());
		stockReqLineUserClass.setLineStatus(stockReqLine.getLineStatus());
		stockReqLineUserClass.setOfficerCode(stockReqLine.getOfficerCode());
		stockReqLineUserClass.setPrice(stockReqLine.getPrice());
		stockReqLineUserClass.setQty(stockReqLine.getQty());
		stockReqLineUserClass.setStoreCode(stockReqLine.getStoreCode());
		stockReqLineUserClass.setTransactionDate(stockReqLine
				.getTransactionDate());

		return stockReqLineUserClass;
	}
}
